<template>
  <!-- 轮播图管理弹窗 -->
  <el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :close-on-press-escape="false" width="600px">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="80px">
      <el-form-item prop="fileName" label="文件名">
        <el-input v-model.trim="dataForm.fileName" placeholder="文件名"></el-input>
      </el-form-item>
      <el-form-item prop="jumpUrl" label="跳转url">
        <el-input v-model.trim="dataForm.jumpUrl" placeholder="跳转url"></el-input>
      </el-form-item>
      <el-form-item prop="useFlag" label="使用标识">
        <el-radio-group v-model="dataForm.useFlag">
          <el-radio value="1" size="large">启用</el-radio>
          <el-radio value="0" size="large">未启用</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item prop="url" label="上传图片">
        <div class="updateImg-box">
          <updateImg ref="uploadRef" :urlList="dataForm.urlList" @updatImgList="updatImgList"></updateImg>
        </div>
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">确定</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref, nextTick } from "vue";
import baseService from "@/service/baseService";
import { IObject } from "@/types/interface";
import { ElMessage } from "element-plus";
import updateImg from "@/components/updateImg.vue";
const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const dataFormRef = ref();
const uploadRef = ref();

const dataForm = reactive<IObject>({
  id: "",
  fileName: "",
  jumpUrl: "",
  useFlag: "0",
  type: 0,
  url: "",
  urlList: []
});

const rules = ref({
  fileName: [{ required: true, message: "必填项不能为空", trigger: "blur" }],
  // jumpUrl: [{ required: true, message: "必填项不能为空", trigger: "blur" }],
  useFlag: [{ required: true, message: "必填项不能为空", trigger: "change" }],
  url: [{ required: true, message: "必填项不能为空", trigger: "change" }]
});

const init = async (id?: number) => {
  visible.value = true;
  dataForm.id = "";

  // 重置表单数据
  if (dataFormRef.value) {
    dataForm.urlList = [];
    dataFormRef.value.resetFields();
  }

  if (id) {
    await getInfo(id);
  }

  await nextTick(() => {
    uploadRef.value.init();
  });
};

// 获取信息
const getInfo = async (id: number) => {
  await baseService.get(`/lingyao/scollimg/${id}`).then((res) => {
    res.data.useFlag = !res.data.useFlag ? "0" : "1";
    res.data.urlList = res.data.url
      ? [
          {
            name: "轮播图",
            url: res.data.url
          }
        ]
      : [];
    Object.assign(dataForm, res.data);
  });
};

// 上传图片成功
const updatImgList = (list: any[]) => {
  dataForm.url = list.length > 0 ? list[0].url : "";
  dataForm.urlList = list;
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    (!dataForm.id ? baseService.post : baseService.put)("/lingyao/scollimg", dataForm).then((res) => {
      ElMessage.success({
        message: "成功",
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
</script>
